<?php
class DateUtils
{
    const ISO_DATETIME_PATTERN = "Y-m-d H:i:s";
    const ISO_DATE_PATTERN = "Y-m-d";
    const SHORT_DATE_PATTERN = "F j, Y";
    const SPECIAL_OFFER_DATE_FORMAT = "F jS";
    const SPECIAL_OFFER_DATE_FORMAT_FULL = "F, j";
    const SPECIAL_OFFER_DATE_FORMAT_FULL_WITH_YEAR = "j M Y";
    const SHORT_DATE_PATTERN_WITH_DAY_SUFFIX = "F jS, Y";

    public static function ConvertDateString($dateStr, $format)
    {
        $date = null;
        try
        {
            $date = new DateTime($dateStr);
        }
        catch (Exception $e)
        {
            $date = new DateTime();
        }
        return $date->format($format);
    }
    
    public static function ConvertTimestampToFormatDate($timestamp, $format)
    {
        return date($format, $timestamp);
    }

    public static function FormatDateToSpecialOffer($date)
    {
        return date(self::SPECIAL_OFFER_DATE_FORMAT, strtotime($date));
    }

    public static function FormatDateToSpecialOfferFull($date)
    {
        return date(self::SPECIAL_OFFER_DATE_FORMAT_FULL, strtotime($date));
    }

    public static function FormatDateToSpecialOfferFullWithYear($date)
    {
        return date(self::SPECIAL_OFFER_DATE_FORMAT_FULL_WITH_YEAR, strtotime($date));
    }

    public static function FormatDateToFullWithYear($date)
    {
        return date(self::SHORT_DATE_PATTERN, strtotime($date));
    }

    public static function AddDate( $timestamp, $years = 0, $months = 0, $days = 0 )
    {
        $date = getdate($timestamp);

        return mktime($date['hours'], $date['minutes'],
                $date['seconds'], $date['mon'] + $months,
                $date['mday'] + $days, $date['year'] + $years);
    }

    public static function SecToMin($seconds)
    {
        $minutes = floor($seconds % 3600 / 60);
        $seconds = $seconds % 60;

        return sprintf("%d:%02d", $minutes, $seconds);
    }
}
?>
